import type { CodeType } from '@/types/user.d'
import { OrderType } from '@/enum'
import {
  cancelOrder,
  deleteOrder,
  followDoctor,
  getPrescriptionPic
} from '@/services/consult'
import { getMedicalOrderDetail } from '@/services/order'
import type { ConsultOrderItem, FollowType } from '@/types/consult'
import type { OrderDetail } from '@/types/order'
import { ImagePreview, Toast, type FormInstance } from 'vant'
import { onMounted, ref, onUnmounted, type Ref } from 'vue'
import { sendMobileCode } from '@/services/user'

// 1. 关注
export const useFollow = (type: FollowType = 'doc') => {
  const loading = ref(false)
  // 类型兼容: 类型多的 可以 传递给 类型少的
  const follow = async (item: { id: string; likeFlag: 0 | 1 }) => {
    loading.value = true
    try {
      await followDoctor(item.id, type)
      item.likeFlag = item.likeFlag === 0 ? 1 : 0
    } finally {
      // 无论请求成功还是失败都要执行
      loading.value = false
    }
  }

  return { loading, follow }
}

// 2. 查看处方
export const useLookPrescription = () => {
  const showPrescription = async (preId?: string) => {
    // console.log(preId)
    if (preId) {
      // 5.1 发请求
      const res = await getPrescriptionPic(preId)
      console.log(res.data)

      // 5.2 预览处方
      ImagePreview([res.data.url])
    }
  }
  return { showPrescription }
}

// 3. 取消订单
export const useCancelOrder = () => {
  const loading = ref(false)
  const cancelConsultOrder = async (item: ConsultOrderItem) => {
    loading.value = true
    // 2.1 调用接口
    try {
      await cancelOrder(item.id)
      Toast.success('取消订单操作成功~')
      // 修改订单数据
      item.status = OrderType.ConsultCancel
      item.statusValue = '已取消'
    } catch (error) {
      Toast.fail('取消订单操作失败~')
    } finally {
      loading.value = false
    }
  }

  return { loading, cancelConsultOrder }
}

// 4. 删除订单
export const useDeleteOrder = (callBack: () => void) => {
  const deleteLoading = ref(false)
  const deleteConsultOrder = async (item: ConsultOrderItem) => {
    deleteLoading.value = true
    try {
      await deleteOrder(item.id)
      Toast.success('删除订单操作成功~')
      // 在不同的组件中要做的事情是不一样的
      // 列表中: 通知父组件去删除数据, 详情中: 回到上一个页面
      callBack && callBack()
    } catch (error) {
      Toast.fail('删除订单操作失败~')
    } finally {
      deleteLoading.value = false
    }
  }
  return { deleteLoading, deleteConsultOrder }
}

// 5. 获取订单详情数据
export const useOrderDetail = (orderId: string) => {
  const order = ref<OrderDetail>()
  const loading = ref(false)
  onMounted(async () => {
    loading.value = true
    try {
      const res = await getMedicalOrderDetail(orderId)
      order.value = res.data
    } finally {
      loading.value = false
    }
  })
  return { order, loading }
}

// 6. 发送短信验证码
export const useSendMobileCode = (
  mobile: Ref<string>,
  type: CodeType = 'login'
) => {
  const time = ref(0)
  const form = ref<FormInstance>()
  let timerId: number
  const send = async () => {
    // 5.1 不在倒计时范围内
    if (time.value > 0) return
    // 5.2 手机号码要正确
    await form.value?.validate('mobile')
    // 5.3 发送短信验证码
    const res = await sendMobileCode(mobile.value, type)
    Toast(res.data.code)

    // 5.4 设置定时器
    time.value = 60
    window.clearInterval(timerId)
    timerId = window.setInterval(() => {
      time.value--
      if (time.value <= 0) window.clearInterval(timerId)
    }, 1000)
  }
  onUnmounted(() => {
    window.clearInterval(timerId)
  })

  return { form, time, send }
}
